//
//  FBTableViewCell.m
//  BandApp
//
//  Created by Alec Flett on 11/8/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "FBTableViewCell.h"


@implementation FBTableViewCell

@synthesize fbr = _fbr;
@synthesize cache = _cache;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
        _cache = [NSURLCache sharedURLCache];
    }
    return self;
}

- (void)dealloc {
    _session = nil;
    [_fbr cancel];
    self.fbr = nil;
    [super dealloc];
}

- (FreebaseSession*)session {
    if (!_session)
        _session = [FreebaseSession session];
    return _session;
}

- (void)setSession:(FreebaseSession*)session {
    _session = session;
}

- (void)setImageData:(id)imageData
{
    self.imageView.autoresizingMask = UIViewAutoresizingNone;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit|UIViewContentModeTopLeft;
    self.imageView.image = [UIImage imageNamed:@"no-fb-image-table.png"];
    if (!imageData || imageData == [NSNull null])
        return;
    
    // if given an array, pick the first one
    if ([imageData isKindOfClass:[NSArray array]])
        imageData = [imageData objectAtIndex:0];
    
    // if given a dict, try to get the id or the guid
    if ([imageData isKindOfClass:[NSDictionary class]]) {
        NSString* image_id = [imageData objectForKey:@"id"];
        if (!image_id)
            image_id = [imageData objectForKey:@"guid"];
        imageData = image_id;
    }
    
    NSInteger rowHeight = 44;
    NSDictionary* imageThumbOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSString stringWithFormat:@"%d", rowHeight], @"maxheight",
                                       [NSString stringWithFormat:@"%d", rowHeight], @"maxwidth",
                                       @"1", @"pad",
                                       @"fit", @"mode",
                                       nil];

    UIImage* directImage = nil;
    if ([_cache respondsToSelector:@selector(imageForURL:)]) {
        NSString *imageUrl = [_session urlForImageThumb:imageData options:imageThumbOptions];
        
        // this sucks, should be built into FreebaseSession
        imageUrl = [NSString stringWithFormat:@"http://%@%@", _session.hostname, imageUrl];
        
        // ignore the warning on this one - not sure how to avoid it, maybe we need to specifically call invoke?
        directImage = [_cache imageForURL:imageUrl];
    }
    
    if (directImage) {
        // in case one was in progress
        [self.fbr cancel];
        self.fbr = nil;
        self.imageView.image = directImage;        
    }
    else {
        self.fbr = [_session
                    imageThumb:imageData
                    name:@"tableImage" 
                    delegate:self
                    didFinishSelector:@selector(imageLoaded:) 
                    options:imageThumbOptions];
    }
}

- (id)imageData
{
    return self.imageView.image;
}

- (void)imageLoaded:(NSData*)imageData
{
    self.fbr = nil;
    UIImage *image = [UIImage imageWithData:imageData];

    self.autoresizesSubviews = NO;
    if (image)
        self.imageView.image = image;
    else
        self.imageView.image = [UIImage imageNamed:@"no-fb-image-table.png"];
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
